home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1953 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.6 KB  |  58 lines

  1. Path: diku.dk!null
  2. From: null@diku.dk (Niels Ull Jacobsen)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Fastest way to index thru an array????
  5. Date: 14 Jan 1996 13:38:04 GMT
  6. Organization: Department of Computer Science, U of Copenhagen
  7. Sender: null@tyr.diku.dk
  8. Message-ID: <4db0vs$6fe@odin.diku.dk>
  9. References: <4d1g3k$o09@solaris.cc.vt.edu>
  10. NNTP-Posting-Host: odin.diku.dk
  11. X-Newsreader: NN version 6.5.0 #13
  12.  
  13. Ashutosh Gokhale <ashutosh> writes:
  14.  
  15. >Suppose I have an array A[][][] which I declare as
  16. >    double ***A and then assign memory to it using new operator.
  17. >Now consider that A has dimensions A[50][60][70]. Then I can index thru ALL
  18. >entries of A using
  19. >for(i=1; i<50; i++)
  20. > {
  21. >   for(j=1; j<60; j++)
  22. >    {
  23. >      for(k=1; k<70; k++)
  24. >       {
  25. >         A[i][j][k] = <some expression>;
  26. >       }
  27. >    }
  28. > }
  29. All the counters should start at 0.
  30. >But the above way is surely the most time-INEFFICIENT way.
  31. >Can someone suggest me the MOST time efficient way of indexing through A[][][]?
  32.  
  33. What you have described is, given any decent compiler, the most
  34. efficient method as the problem is formulated. 
  35.  
  36. The only real improvement you could make was if you could control the 
  37. allocation of A to make sure that the rows were laid out in order.
  38. That is, declaring A as:
  39. double * A[50][60];
  40.  
  41. pMem = new double[50*60*70];
  42. for(i=0; i<50;i++)
  43.   for(j=0;j<60;j++)
  44.      A[i][j] = &(pMem[(i*60*+j)*70]);
  45.  
  46. In that case you could loop through A with a simple
  47.  
  48. for(k=0; k<50*60*70; k++)
  49.    A[0][0][k] = <some expression>;
  50.  
  51.  
  52. >Thanks a lot
  53.  
  54. -- 
  55.    Niels Ull Jacobsen, Dep. of CS, U of Copenhagen (null@diku.dk)
  56.    Roenne Alle 3 st.th, 2860 Soeborg, Denmark, tel. +45 39 66 39 86
  57.  
  58.